#!/usr/bin/env python
# coding=utf-8


import datetime

#from lock import SECRET

from google.appengine.ext import db
from google.appengine.api import memcache
from google.appengine.api import users

class Article(db.Model):
  title = db.StringProperty(required=False, indexed=True)
  title_link = db.StringProperty(required=False, indexed=True)
  title_url = db.StringProperty(required=False, indexed=True)
  parent_url = db.StringProperty(required=False, indexed=True)
  is_page = db.BooleanProperty(required=True, default=False)
  is_for_sidebar = db.BooleanProperty(required=True, default=False)
  content = db.TextProperty()
  content_formatted = db.TextProperty()
  format = db.StringProperty(required=True, default='html', indexed=True)
  created = db.DateTimeProperty(auto_now_add=True)
  last_modified = db.DateTimeProperty(auto_now=True)
  hits = db.IntegerProperty(default=0)
  #category = db.ReferenceProperty(Category)
  #article_list = db.ListProperty(db.Key)
  have_comment = db.BooleanProperty(required=True, default=False)
  article_list = db.StringListProperty() #why? use to backup

  def allcategory(self):
    allc = []
    for url in self.article_list:
      cate = db.GqlQuery("SELECT * FROM Category WHERE category_url = :1", url)
      if cate.count() == 1:
        cate = cate[0]
        c = '''<div id="cayegory-%s" class="category" <a href="/%s" >%s</a>''' % (cate.name,cate.category_url,cate.name)
        allc.append(c)
    return allc
    allcategory = classmethod(allcategory)

class Category(db.Model):
  name = db.StringProperty(required=False, indexed=True)
  category_url = db.StringProperty(required=False, indexed=True)#设置为固定？

#  @property
  def categoryall(self):
    return Article.gql("WHERE article_list = :1 ORDER BY created DESC", self.category_url) #why? use to backup
#    return Article.gql("WHERE article_list = :1", self.key())
    categoryall = classmethod(categoryall)

class Admin(db.Model):
  author_name = db.StringProperty(required=False, indexed=True)
  author_email = db.StringProperty(required=False, indexed=True)
  author_password = db.TextProperty()

class Comment(db.Model):
  author_name = db.StringProperty(required=False, indexed=True)
  author_email = db.StringProperty(required=False, indexed=True)
  comment_parent_url = db.StringProperty(required=False, indexed=True)
  comment_content = db.TextProperty()

class Datum(db.Model):
  title = db.StringProperty(required=False, indexed=True)
  substance = db.TextProperty()
  alist = db.StringListProperty()
  
  def get(self, name):
    value = memcache.get('datum_' + name)
    alist = memcache.get('datum_alist_' + name)#new
    if value and alist is not None:
      return value,alist
    else:
      q = db.GqlQuery("SELECT * FROM Datum WHERE title = :1", name)
      if q.count() == 1:
        value = q[0].substance
        alist = q[0].alist #new
        memcache.delete('datum_' + name)
        memcache.delete('datum_alist_' + name)#new
        memcache.set('datum_' + name, value, 86400)
        memcache.set('datum_alist_' + name, alist, 86400)#new
      return value,alist
  get = classmethod(get)
    
  def set(self, name, value, alist=['c','c','d','j','h']):
    q = db.GqlQuery("SELECT * FROM Datum WHERE title = :1", name)
    if q.count() == 1:
      d = q[0]
    else:
      d = Datum()
      d.title = name
    d.substance = value
    d.alist = alist
    d.put()
    
    memcache.delete('datum_' + name)
    memcache.delete('datum_alist_' + name)#new
    memcache.set('datum_' + name, d.substance, 86400)
    memcache.set('datum_alist_' + name, d.alist, 86400)#new
  set = classmethod(set)
